﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Skywave.Reporting
{
    public class HtmlReportFieldHtmlTable : HtmlReportField
    {
        public List<DataStructure.Pair<string, string>> Template_Rows { get; set; }
        public List<DataStructure.Pair<string, string>> Template_Headers { get; set; }
        public string Template_Table { get; set; }

        private int? _PagingRowsPerPage = null;
        /// <summary>
        /// ...
        /// </summary>
        public int? PagingRowsPerPage
        {
            get { return _PagingRowsPerPage; }
            set { _PagingRowsPerPage = value; }
        }

        private bool? _PagingRepeatWholeTable = null;
        /// <summary>
        /// ...
        /// </summary>
        public bool? PagingRepeatWholeTable
        {
            get { return _PagingRepeatWholeTable; }
            set { _PagingRepeatWholeTable = value; }
        }

        int iPage = 0;
        /// <summary>
        /// ...
        /// </summary>
        public int Paging_CurrentPage
        {
            get { return iPage + 1; }
        }

        internal override string GetContent(HtmlReportFieldGetContentArgs e)
        {
            string r = "";
            string s1;
            string r_Page_Template = "";
            //
            HtmlReportFieldGetContentArgs e_Clone = e.Clone() as HtmlReportFieldGetContentArgs;
            //
            r = Template_Table;
            if (Template_Headers != null)
            {
                foreach (var fe1 in Template_Headers)
                {
                    s1 = fe1.Data2;
                    foreach (var fe2 in ParsedFields.Where(q => fe1.Data2.Contains(q.ParsedText_ReplaceKey)))
                    {
                        e_Clone.Value = e.Report.GetFieldValue(fe2.Field, e.DataItem);
                        s1 = s1.Replace(fe2.ParsedText_ReplaceKey, fe2.Field.GetContent(e_Clone));
                    }
                    //
                    if (PagingRepeatWholeTable == true)
                    {
                        r = r.Replace(fe1.Data1, s1);
                    }
                    else
                    {
                        r_Page_Template += s1;
                        r = r.Replace(fe1.Data1, "");
                    }
                }
            }
            //
            if (PagingRepeatWholeTable == true)
            {
                r_Page_Template = r;
                r = "";
            }
            //
            string r_Rows = "";
            if (Template_Rows.Count > 0)
            {
                iPage = 0;
                string r_Page;
                int iTemplateRow = 0;
                string sTemplateRow;
                if (e.Value is IEnumerable)
                {
                    List<object> eValues = (e.Value as IEnumerable).Cast<object>().ToList();
                    int rowsPerPage = (PagingRowsPerPage == null) ? eValues.Count : PagingRowsPerPage.Value;
                    //
                    while (true)
                    {
                        if (PagingRepeatWholeTable == true)
                        {
                            string r_PageTemplate_New = Template_Table;
                            if (Template_Headers != null)
                                foreach (var fe1 in Template_Headers)
                                {
                                    s1 = fe1.Data2;
                                    foreach (var fe2 in ParsedFields.Where(q => fe1.Data2.Contains(q.ParsedText_ReplaceKey)))
                                    {
                                        e_Clone.Value = e.Report.GetFieldValue(fe2.Field, e.DataItem);
                                        s1 = s1.Replace(fe2.ParsedText_ReplaceKey, fe2.Field.GetContent(e_Clone));
                                    }
                                    //
                                    if (PagingRepeatWholeTable == true)
                                    {
                                        r_PageTemplate_New = r_PageTemplate_New.Replace(fe1.Data1, s1);
                                    }
                                    else
                                    {
                                        r_Page_Template += s1;
                                        r_PageTemplate_New = r_PageTemplate_New.Replace(fe1.Data1, "");
                                    }
                                }
                            r_Page_Template = r_PageTemplate_New;
                        }
                        r_Page = r_Page_Template;
                        if (PagingRepeatWholeTable == true)
                            r_Rows = "";
                        foreach (var feValue in eValues.Skip(iPage * rowsPerPage).Take(rowsPerPage))
                        {
                            sTemplateRow = Template_Rows[iTemplateRow].Data2;
                            foreach (var fe1 in ParsedFields.Where(q => sTemplateRow.Contains(q.ParsedText_ReplaceKey)))
                            {
                                e_Clone.Value = e.Report.GetFieldValue(fe1.Field, feValue);
                                sTemplateRow = sTemplateRow.Replace(fe1.ParsedText_ReplaceKey, fe1.Field.GetContent(e_Clone));
                            }
                            //
                            if (PagingRepeatWholeTable == true)
                                r_Rows += sTemplateRow + "\n";
                            else
                                r_Page += sTemplateRow + "\n";
                            //
                            iTemplateRow++;
                            if (iTemplateRow >= Template_Rows.Count)
                                iTemplateRow = 0;
                        }
                        //
                        if (PagingRepeatWholeTable == true)
                        {
                            for (int i1 = 1; i1 < Template_Rows.Count; i1++)
                            {
                                r_Page = r_Page.Replace(Template_Rows[i1].Data1, "");
                            }
                            r_Page = r_Page.Replace(Template_Rows[0].Data1, r_Rows);
                            //
                            foreach (var fe1 in ParsedFields.Where(q => r_Page.Contains(q.ParsedText_ReplaceKey)))
                            {
                                e_Clone.Value = e.Report.GetFieldValue(fe1.Field, e.DataItem);
                                r_Page = r_Page.Replace(fe1.ParsedText_ReplaceKey, fe1.Field.GetContent(e_Clone));
                            }
                            //
                            r += r_Page;
                        }
                        else
                            r_Rows += r_Page;
                        //
                        iPage++;
                        //
                        if ((iPage * rowsPerPage) >= eValues.Count)
                            break;
                        else
                        {
                            if (PagingRepeatWholeTable == true)
                                r += "<table style='page-break-after: always;' />";
                            else
                                r_Rows += "<tr style='page-break-after: always;' />";
                        }
                    }
                    ////
                    //foreach (var feValue in e.Value as IEnumerable)
                    //{
                    //    sTemplateRow = Template_Rows[iTemplateRow].Data2;
                    //    foreach (var fe1 in ParsedFields.Where(q => sTemplateRow.Contains(q.ParsedText_ReplaceKey)))
                    //    {
                    //        e_Clone.Value = e.Report.GetFieldValue(fe1.Field, feValue);
                    //        sTemplateRow = sTemplateRow.Replace(fe1.ParsedText_ReplaceKey, fe1.Field.GetContent(e_Clone));
                    //    }
                    //    //
                    //    r_Rows += sTemplateRow + "\n";
                    //    iTemplateRow++;
                    //    if (iTemplateRow >= Template_Rows.Count)
                    //        iTemplateRow = 0;
                    //}
                }
                //
                if (!(PagingRepeatWholeTable == true))
                {
                    for (int i1 = 1; i1 < Template_Rows.Count; i1++)
                    {
                        r = r.Replace(Template_Rows[i1].Data1, "");
                    }
                    r = r.Replace(Template_Rows[0].Data1, r_Rows);
                }
            }
            //
            foreach (var fe1 in ParsedFields.Where(q => r.Contains(q.ParsedText_ReplaceKey)))
            {
                e_Clone.Value = e.Report.GetFieldValue(fe1.Field, e.DataItem);
                r = r.Replace(fe1.ParsedText_ReplaceKey, fe1.Field.GetContent(e_Clone));
            }
            //
            return r;
        }

        internal override void Create_Internal(string toParse)
        {
            using (System.IO.StringReader sr1 = new System.IO.StringReader(HtmlReport.Parsing_FixToParse(toParse)))
            {
                System.Xml.XmlReader xmlR1 = HtmlReport.Parsing_GetXmlReader(sr1);
                bool isStarted = false;
                string s1, s2;
                //
                while (xmlR1.Read())
                {
                    if (xmlR1.IsStartElement("ReportField", "http://skywavetech.ir/skywave/reporting"))
                    {
                        //
                        s1 = xmlR1.GetAttribute("PagingRowsPerPage");
                        if (!string.IsNullOrEmpty(s1))
                            _PagingRowsPerPage = int.Parse(s1);
                        //
                        s1 = xmlR1.GetAttribute("PagingRepeatWholeTable");
                        if (!string.IsNullOrEmpty(s1))
                            _PagingRepeatWholeTable = bool.Parse(s1);
                        //
                        isStarted = true;
                    }
                    if (isStarted)
                    {
                        if (xmlR1.IsStartElement("Header"))
                        {
                            if (Template_Headers == null)
                                Template_Headers = new List<DataStructure.Pair<string, string>>();
                            s1 = xmlR1.GetAttribute("Value");
                            s2 = xmlR1.ReadOuterXml();
                            if (string.IsNullOrEmpty(s1))
                                s1 = s2.Replace("<Header>", "").Replace("</Header>", "");
                            //
                            Template_Headers.Add(new DataStructure.Pair<string, string>(s2, s1));
                        }
                        else if (xmlR1.IsStartElement("Row"))
                        {
                            if (Template_Rows == null)
                                Template_Rows = new List<DataStructure.Pair<string, string>>();
                            s1 = xmlR1.GetAttribute("Value");
                            s2 = xmlR1.ReadOuterXml();
                            if (string.IsNullOrEmpty(s1))
                                s1 = s2.Replace("<Row>", "").Replace("</Row>", "");
                            //
                            Template_Rows.Add(new DataStructure.Pair<string, string>(s2, s1));
                        }
                    }
                }
            }
            //
            using (System.IO.StringReader sr1 = new System.IO.StringReader(HtmlReport.Parsing_FixToParse(toParse)))
            {
                System.Xml.XmlReader xmlR1 = HtmlReport.Parsing_GetXmlReader(sr1);
                //
                while (xmlR1.Read())
                {
                    if (xmlR1.Depth == 0 && xmlR1.IsStartElement("ReportField", "http://skywavetech.ir/skywave/reporting"))
                    {
                        Template_Table = xmlR1.ReadInnerXml();
                        break;
                    }
                }
            }
        }
    }
}
